const puppeteer = require('puppeteer');
const url = `https://movie.douban.com/tag/#/?sort=R&range=0,10&tags=`;

(async () => {
    console.log('puppeteer start runing');

    const browser = await puppeteer.launch(
        // {headless: false} // false时 可以 查看 爬取数据 的ui 交互过程
    );

    const page = await browser.newPage();

    await page.goto(url, {
        waitUntil: 'networkidle2'
    });

    await page.waitFor(2000);

    await page.waitForSelector('.more'); // 一直等到 class .more 出来后  再爬取 

    for (let i = 0; i < 2; i++) {
        console.log(i);
        await page.waitFor(2000);
        await page.click('.more');
    }

    /**
     * 获取资料 result
     */
    const result = await page.evaluate(() => {
        let items = document.querySelectorAll('.list-wp a');
        let links = [];

        if (items.length >= 1) {
            items.forEach(async (item, index) => {
                let poster = item.querySelector('div.cover-wp')
                    .querySelector('.pic')
                    .querySelector('img').src;
                let doubanId = item.querySelector('div.cover-wp').dataset.id;
                let title = item.querySelector('p .title').innerHTML;
                let rate = Number(item.querySelector('p .rate').innerHTML);

                poster = poster ? poster.replace('s_ratio', 'l_ratio') : ''; // 小图 转高清图片

                links.push({
                    doubanId,
                    poster,
                    title,
                    rate
                });
            });
        }

        console.log(links);
        return links;
    });

    browser.close();
    process.send({ result });
    process.exit(0);

})()